#! /bin/sh
# PCP QA Test No. 461
# checks pmdalogger event access functionality
#
# Copyright (c) 2011 Aconex.  All Rights Reserved.
#
seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

[ -d $PCP_PMDAS_DIR/logger ] || _notrun "No pmdalogger installed"

_cleanup()
{
    [ -f $PCP_VAR_DIR/config/logger/logger.conf.$seq ] && \
    _restore_config $PCP_VAR_DIR/config/logger/logger.conf
    _restore_pmda_install logger
    if $_needclean
    then
	if $install_on_cleanup
	then
	    ( cd $PCP_PMDAS_DIR/logger; $sudo ./Install </dev/null >/dev/null 2>&1 )
	else
	    ( cd $PCP_PMDAS_DIR/logger; $sudo ./Remove </dev/null >/dev/null 2>&1 )
	fi
	_needclean=false
    fi
    $sudo rm -f $tmp.*
    exit $status
}

_testdata()
{
    # generate data in a single write, for pmda determinism
    echo \
"TeSt DaTa ... TeSt DaTa ...
TeSt2 DaTa2 ... TeSt2 DaTa2"
}

_filter()
{
    sed \
	-e "s,$tmp,TMPFILE,g" \
	-e "s,$PCP_VAR_DIR,PCP_VAR_DIR,g" \
	-e "s/host:      $host/host:      localhost/g" \
	-e 's/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\.[0-9][0-9]*[0-9]/TIMESTAMP/g'
}

install_on_cleanup=false
pminfo logger >/dev/null 2>&1 && install_on_cleanup=true

status=1	# failure is the default!
host=`hostname`
_needclean=true
trap "_cleanup" 0 1 2 3 15

# real QA test starts here
_prepare_pmda_install logger

# prepare test case files (see below)
$sudo rm -f $tmp.*
touch  $tmp.reg

echo "
reg	y	$tmp.reg
" > $tmp.conf
[ -d $PCP_VAR_DIR/config/logger ] || $sudo mkdir -p $PCP_VAR_DIR/config/logger
[ -f $PCP_VAR_DIR/config/logger/logger.conf ] && \
_save_config $PCP_VAR_DIR/config/logger/logger.conf
$sudo cp $tmp.conf $PCP_VAR_DIR/config/logger/logger.conf

# install PMDA accepting the defaults (esp config file)
$sudo ./Remove < /dev/null >/dev/null 2>&1
$sudo ./Install < /dev/null >$tmp.out 2>&1
cat $tmp.out | _filter_pmda_install | _filter

echo "=== 1. store access control enabled ==="
echo "Starting initial event watcher:"
pmevent -x '.' -s 5 -t 1 logger.perfile.reg.records > $tmp.event &
sleep 2
_testdata >> $tmp.reg
wait
echo "done."
echo "Captured event trace was:"
cat $tmp.event | _filter

echo "=== 2. store access control disabled ==="
echo "Starting initial event watcher:"
pmevent -s 5 -t 1 logger.perfile.reg.records > $tmp.event &
sleep 2
_testdata >> $tmp.reg
wait
echo "done."
echo "Captured event trace was:"
cat $tmp.event | _filter

status=0
exit
